<template>
	<section>
		<!-- 工具条 +关键字查询+新增-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" >
				<el-form-item>
					<el-input v-model="keyword" placeholder="关键字查询"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="keywordQuery" icon="el-icon-zoom-in">查询</el-button>
				</el-form-item>

				<el-form-item>
					<el-button type="primary" @click="handleAdd" icon="el-icon-circle-plus">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>
    <!--

        @row-dblclick="rowDrawerClick" 点击事件  点击弹出字典明细表数据
    -->
		<!--列表数据-->
		<el-table :data="systemdictionarytypes" highlight-current-row v-loading="listLoading"
              @selection-change="selsChange" :row-key="getKey"  ref="multiTable"
              @row-dblclick="rowDrawerClick"
              style="width: 100%;">
			<el-table-column type="selection" width="55" :reserve-selection="true">
			</el-table-column>
			<el-table-column type="index" label="序号"width="60">
			</el-table-column>
			<el-table-column prop="sn" label="数据字典类型编号" width="200" sortable>
			</el-table-column>
			<el-table-column prop="name" label="数据字典类型名称" width="200"  sortable>
			</el-table-column>
			<el-table-column label="操作">
				<template scope="scope">
					<el-button type="warning" size="small" @click="handleEdit(scope.$index, scope.row)" icon="el-icon-edit-outline">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)" icon="el-icon-delete">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条+批量删除+分页栏-->
    <!--
    :disabled="this.sels.length===0"
    当sels长度为0的时候，显示当前属性disabled - 禁用。如果不为0就不禁用
   -->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0" icon="el-icon-finished">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                     :page-size="pageSize" :total="totals" :current-page="currentPage"
                     style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增+编辑界面-->
    <!--
      title="添加或修改" - 标题
      :visible.sync="saveFormVisible" - 控制当前对话框是否显示 - 默认为false不显示
      :close-on-click-modal="false" - 点击对话框以外的区域不关闭
      -->
		<el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false"
               :destroy-on-close="true">
			<el-form :model="saveForm" label-width="150px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="数据字典类型编号" prop="sn">
          <el-input v-model="saveForm.sn" placeholder="请输入数据字典类型编号" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="数据字典类型名称" prop="name">
					<el-input v-model="saveForm.name" placeholder="请输入数据字典类型名称" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>

			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false" icon="el-icon-close">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading"icon="el-icon-check">提交</el-button>
			</div>
		</el-dialog>

<!--    下面是字典明细表的功能 --------------主表有个点击事件--------------------------------------------------->
    <!--
      :close-on-click-modal="false" - 点击对话框以外的区域不关闭
      -->
    <div class="demo-drawer__content">
      <el-drawer  :title="title2"   width="500px" :visible.sync="innerDrawer"
                  :before-close="handleClose"
                  v-loading="listLoading2" direction="rtl" size="40%">
        <!--工具条 +关键字查询+新增-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
          <el-form :inline="true" >
            <el-form-item>
              <el-input v-model="keyword2" placeholder="关键字查询"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary"  v-on:click="keywordQuery2" icon="el-icon-zoom-in">查询</el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="primary"  @click="handleAdd2" icon="el-icon-circle-plus">新增</el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="success"  @click="renovate2" icon="el-icon-loading">刷新</el-button>
            </el-form-item>
          </el-form>
        </el-col>

      <!--  字典明细列表展示      -->
        <el-table :data="SystemdictionarydetailData" v-loading="listLoading2" :close-on-click-modal="true"  highlight-current-row
                  @selection-change="selsChange2"  :row-key="getKey2"  ref="multiTable"  >
          <el-table-column type="selection" width="55" :reserve-selection="true"></el-table-column>
          <el-table-column type="index" label="序号" width="60">
          </el-table-column>
          <el-table-column property="name" label="字典名称" width="150"></el-table-column>
          <el-table-column property="systemdictionarytype.name" label="字段类型" width="200"></el-table-column>
          <el-table-column label="操作" >
            <template scope="scope">
              <el-button type="warning"size="small"  @click="handleEdit2(scope.$index, scope.row)" icon="el-icon-edit-outline">编辑</el-button>
              <el-button type="danger" size="small"  @click="handleDel2(scope.$index, scope.row)" icon="el-icon-delete">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--工具条+批量删除+分页栏-->
        <el-col :span="24" class="toolbar">
          <el-button type="danger" @click="batchRemove2" :disabled="this.sels2.length===0" icon="el-icon-finished">批量删除</el-button>
          <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange2"
                         :page-size="pageSize2" :total="totals2" :current-page="currentPage2"
                         style="float:right;">
          </el-pagination>
        </el-col>



      </el-drawer>

      <!--新增+编辑界面-->
      <el-dialog :title="titleEdit2"  :visible.sync="saveFormVisible2" :close-on-click-modal="false"
                 :destroy-on-close="true">
        <el-form :model="saveForm2" label-width="150px" :rules="saveFormRules2" ref="saveForm2">
          <el-form-item label="明细名称" prop="name">
            <el-input v-model="saveForm2.name" placeholder="请输入数据字典明细名称" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="明细类型"  prop="typesId">
            <el-select v-model="saveForm2.typesId" clearable placeholder="请选择数据字典明细类型">
              <el-option v-for ="item in dictionarytypes"
                         :label="item.name"
                         :value="item.id"
                         clearable>
                <span style="float: left">{{ item.name }} </span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click.native="saveFormVisible2 = false" icon="el-icon-close">取消</el-button>
          <el-button type="primary" @click.native="saveSubmit2" :loading="saveLoading2" icon="el-icon-check">提交</el-button>
        </div>
      </el-dialog>


    </div>

	</section>
</template>

<script>

	export default {
		data() {
			return {
			  //1.分页列表展示的相关数据
        systemdictionarytypes: [],   //列表数据
        listLoading: false,//关闭加载框
        pageSize:5,        //当前页数
        currentPage:1,     //当前页
        totals:0,          //总条数

        //2.关键字查询
        keyword:'',

        //3.新增/修改
        saveFormVisible: false,//新增/编辑界面是否显示
        title:'',             //新增/修改各自变换标题
        saveForm: {           //新增/修改模型数据
          id: null,
          sn: '',
          name: ''
        },
        saveLoading: false,     //保存的加载框 - 默认不显示

        //4.批量删除
        sels: [],//列表选中列


        //5.校验表单项里面的数据u
        saveFormRules: {
          name: [
            { required: true, message: '请输入数据字典类型名称', trigger: 'blur' },
            { required: true, message: '请输入数据字典类型名称', trigger: 'change' }
          ],
          sn: [
             { required: true, message: '请输入数据字典类型编号', trigger: 'blur' },
            { required: true, message: '请输入数据字典类型编号', trigger: 'change' }
          ],
        },
        //下拉选的类型的模型数据
        dictionarytypes:{},

//-----抽屉的模型数据u------明细表的模型型数据-------------------
        //多层抽屉
        innerDrawer: false,
        //抽屉数据列表---数据字典明细表
        SystemdictionarydetailData: [],
        //每页数据数组
        pageSizes: [5, 10, 15, 20],
        //-----------------------------------------
        //1.分页列表展示的相关数据
        listLoading2: false,//关闭加载框
        pageSize2:5,        //当前页数
        currentPage2:1,     //当前页
        totals2:0,          //总条数

        //2.关键字查询
        keyword2:'',

        //3.新增/修改
        saveFormVisible2: false,//新增/编辑界面是否显示
        title2:'',             //指定是字典明细
        titleEdit2:'', //新增/修改各自变换标题
        saveForm2: {           //新增/修改模型数据
          id: null,
          name: '',
          typesId: null,
        },
        saveLoading2: false,     //保存的加载框 - 默认不显示
        typesId: '',
        //4.批量删除
        sels2: [],//列表选中列

        //6.查询数据字典类型
        systemdictionarytypes2:{},

        //7.校验表单项里面的数据
        saveFormRules2: {
          name: [
             { required: true, message: '请输入数据字典明细名称', trigger: 'blur' },
            { required: true, message: '请输入数据字典明细名称', trigger: 'change' }
          ]

        },

      }
		},
		methods: {

      //1.获取用户列表---分页
      getSystemdictionarytypes() {
        let para = {
          pageSize: this.pageSize,        //当前的页数
          currentPage: this.currentPage,  //当前页
          keyword: this.keyword,          //关键字查询
        };
        this.listLoading = true ;         //打开加载框
        this.$http.post("/systemdictionarytype",para).then(res=>{
          this.systemdictionarytypes=res.data.data; //给模型数据赋值
          this.totals=res.data.totals;    //计算总条数
          this.listLoading = false;       //关闭加载框
        }).catch(res=>{
          this.$message.error("系统繁忙，请稍后再试【400,404】")
        })

      },

      //2.点击分页实现
      handleCurrentChange(currentPage) {
        this.currentPage = currentPage;
        this.getSystemdictionarytypes();
      },

      //3.高级查询
      keywordQuery(){
        if (this.keyword==''){
          this.$message.warning("请输入关键字进行高级查询!!!");
          return;
        };
        this.currentPage = 1;
        this.getSystemdictionarytypes();
      },

      //4.显示新增界面
      handleAdd: function () {
        this.saveFormVisible = true;
        this.title="新增",
        //重置from表单
        this.saveForm = {
          id: null,
          sn: '',
          name: ''
        }
      },

      //5.显示修改界面
      handleEdit: function (index, row) {
        this.title="修改";
        //弹出对话框
        this.saveFormVisible = true;
        //回显数据：创建一个新的空对象{}，然后将row中的数据拷贝一份给{}
        this.saveForm = Object.assign({}, row);

      },

      //6..删除功能逻辑
      handleDel: function (index, row) {
        //elementUi提供确认框 - 点确定之后执行then里面的代码，点击取消执行catch中的逻辑
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true; //显示加载框
          this.$http.delete("/systemdictionarytype/"+row.id).then(res=>{
            this.listLoading = false; //关闭加载框
            if(res.data.success){
              this.$message.success("删除成功"); //提示成功消息

              //删除成功之后对当前页的处理【特殊情况：删除最后一页的最后一条数据】
              var totalPage = Math.ceil(this.totals/this.pageSize); //总页数 = 最后一页
              //如果当前页是最后一页                  并且是最后一条数据                  并且不是第一页
              if(this.currentPage==totalPage && (this.totals-1)%this.pageSize==0 && this.currentPage!=1){
                this.currentPage--;
              }
              this.getSystemdictionarytypes();
            }else{
              this.$message.error("系统繁忙，请稍后重试!!![500]"); //后端500错误走这里
            }
          }).catch(res=>{
            this.$message.error("系统繁忙，请稍后重试!!![400,404]");
          })
        }).catch(() => {
          this.$message.info("已取消操作");
          this.$refs.multiTable.clearSelection();
        });


      },

      //7.批量删除
      selsChange: function (sels) {
        this.sels = sels;
      },

      //7.跨页批量删除
      getKey(row){
        return row.id;},

      //7.批量删除逻辑
      batchRemove: function () {
        //获取数组中每一个对象的id 并形成新的数组
        var ids = this.sels.map(item => item.id);
        this.$confirm('确认批量删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true; //显示加载框
          this.$http.patch("/systemdictionarytype",ids).then(res=>{
            this.listLoading = false; //隐藏加载框
            if(res.data.success){ //操作成功
              this.$message.success("批量删除成功"); //提示成功消息
              //清空批量删除的事件
              this.$refs.multiTable.clearSelection();
              //批量删除后---当前页值为1
              this.currentPage=1;
              this.getSystemdictionarytypes();

            }else{ //操作失败
              this.$message.error("系统繁忙，请稍后重试!!![500]"); //后端500错误走这里
            }
          }).catch(res=>{
            this.$message.error("系统繁忙，请稍后重试!!![400,404]");
          })
        }).catch(() => {
          this.$message.info("已取消操作");

        });
      },

      //8.提交新增表单数据   点击保存提交数据到后端
      saveSubmit: function () {
        //表单验证：表单项中任何一个验证不通过，都不能点击提交
        //所有的ref中名称为saveForm的表单
        this.$refs.saveForm.validate((valid) => {
          if (valid) { //表单验证通过 => 每一个表单项都通过了
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.saveLoading = true; //显示加载框
              this.$http.put("/systemdictionarytype",this.saveForm).then(res=>{
                this.saveLoading = false;  //关闭加载框
                this.saveFormVisible = false; //关闭对话框
                if(res.data.success){
                  this.$message.success(this.title+" : 操作成功"); //提示成功消息
                  this.getSystemdictionarytypes();    //重新加载新列表的数据
                }else{
                  //this.$message.error("系统繁忙，请稍后重试!!![500]");
                  //打印后端的错误信息
                  this.$message.error(res.data.message);
                }
              }).catch(res=>{
                this.saveLoading = false;  //关闭加载框
                this.saveFormVisible = false; //关闭对话框
                this.$refs.saveForm.clearValidate();//清除提示
                this.$message.error("系统繁忙，请稍后重试!!![400,404]");
              })
            });
          }
        });
      },
//**********************下面是明细表的方法******************************************************************************
      //1.双击显示内层抽屉
      rowDrawerClick(row) {
        console.debug(row)
        this.typesId=row.id;
        this.title2="数据字典类型明细";
        //每次子表打开，页码重新赋值为1
        this.currentPage2 = 1;
        //每次子表打开，每页数据条数重新赋值为5条
        this.pageSize2 = 5;
        //清空query
        this.keyword2='',
        //展示内层抽屉列表
        this.getSystemdictionarydetails(this.typesId);
        //弹出内层抽屉
        this.innerDrawer = true
      },

      //1.获取用户列表---分页
      getSystemdictionarydetails() {
        let para = {
          pageSize: this.pageSize2,        //当前的页数
          currentPage: this.currentPage2,  //当前页
          keyword: this.keyword2,          //关键字查询
          typesId:this.typesId,
        };
        this.listLoading2 = true ;         //打开加载框
        this.$http.post("/systemdictionarydetail",para).then(res=>{
          this.SystemdictionarydetailData=res.data.data; //给模型数据赋值
          this.totals2=res.data.totals;    //计算总条数
          this.listLoading2 = false;       //关闭加载框
          this.listLoading = false;
        }).catch(res=>{
          this.$message.error("系统繁忙，请稍后再试【400,404】")
        })

      },

      //2.点击分页实现
      handleCurrentChange2(currentPage) {
        this.currentPage2 = currentPage;
        this.getSystemdictionarydetails();
      },

      //3.高级查询
      keywordQuery2(){
        if (this.keyword2==''){
          this.$message.warning("请输入关键字进行高级查询!!!");
          return;
        };
        this.currentPage2 = 1;
        this.getSystemdictionarydetails();
      },

      //2.内层抽屉关闭提示
      handleClose(done) {
        this.$confirm('确定关闭吗？')
            .then(_ => {
              done();
            })
            .catch(_ => {
            });
      },
      //刷新按钮功能
      renovate2(){
        this.getSystemdictionarydetails();
      },
      //3.显示新增界面
      handleAdd2: function () {
        this.saveFormVisible2 = true;
        this.titleEdit2="新增",
            //重置from表单
            this.saveForm2 = {
              id: null,
              name: '',
              typesId: null,
            };
        this.saveFormRules='';
        //查询数据字典类型
        this.getDictionarytypes();
      },
      //4.显示修改界面
      handleEdit2: function (index, row) {
        this.titleEdit2="修改";
        //弹出对话框
        this.saveFormVisible2 = true;
        //回显数据：创建一个新的空对象{}，然后将row中的数据拷贝一份给{}
        this.saveForm2 = Object.assign({}, row);
        //查询数据字典类型
        this.getDictionarytypes();
      },

      //5.删除功能逻辑
      handleDel2: function (index, row) {
        //elementUi提供确认框 - 点确定之后执行then里面的代码，点击取消执行catch中的逻辑
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading2 = true; //显示加载框
          this.$http.delete("/systemdictionarydetail/"+row.id).then(res=>{
            this.listLoading2 = false; //关闭加载框
            if(res.data.success){
              this.$message.success("删除成功"); //提示成功消息

              //删除成功之后对当前页的处理【特殊情况：删除最后一页的最后一条数据】
              var totalPage = Math.ceil(this.totals2/this.pageSize2); //总页数 = 最后一页
              //如果当前页是最后一页                  并且是最后一条数据                  并且不是第一页
              if(this.currentPage2==totalPage && (this.totals2-1)%this.pageSize2==0 && this.currentPage2!=1){
                this.currentPage2--;
              }
              this.getSystemdictionarydetails();//重新家加载列表
            }else{
              this.$message.error("系统繁忙，请稍后重试!!![500]"); //后端500错误走这里
            }
          }).catch(res=>{
            this.$message.error("系统繁忙，请稍后重试!!![400,404]");
          })
        }).catch(() => {
          this.$message.info("已取消操作");
          this.$refs.multiTable.clearSelection();
        });
      },

      //7.批量删除
      selsChange2: function (sels) {
        this.sels2 = sels;
      },
      //7.跨页批量删除
      getKey2(row){
        return row.id;},
      //7.批量删除逻辑
      batchRemove2: function () {
        //获取数组中每一个对象的id 并形成新的数组
        var ids = this.sels2.map(item => item.id);
        this.$confirm('确认批量删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading2 = true; //显示加载框
          this.$http.patch("/systemdictionarydetail",ids).then(res=>{
            this.listLoading2 = false; //隐藏加载框
            if(res.data.success){ //操作成功
              this.$message.success("批量删除成功"); //提示成功消息
              //清空批量删除的事件
              this.$refs.multiTable.clearSelection();
              //批量删除后---当前页值为1
              this.currentPage2=1;
              this.getSystemdictionarydetails();//重新家加载列表
            }else{ //操作失败
              this.$message.error("系统繁忙，请稍后重试!!![500]"); //后端500错误走这里
            }
          }).catch(res=>{
            this.$message.error("系统繁忙，请稍后重试!!![400,404]");
          })
        }).catch(() => {
          this.$message.info("已取消操作");

        });
      },
      //8.提交新增表单数据   点击保存提交数据到后端
      saveSubmit2: function () {
        //表单验证：表单项中任何一个验证不通过，都不能点击提交
        //所有的ref中名称为saveForm的表单
        this.$refs.saveForm2.validate((valid) => {
          if (valid) { //表单验证通过 => 每一个表单项都通过了
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.saveLoading2 = true; //显示加载框
              let para = Object.assign({}, this.saveForm2);

              this.$http.put("/systemdictionarydetail",para).then(res=>{
                this.saveLoading2 = false;  //关闭加载框
                this.saveFormVisible2 = false; //关闭对话框
                if(res.data.success){
                  this.$message.success(this.titleEdit2+" : 操作成功"); //提示成功消息
                  this.getSystemdictionarydetails();    //重新加载新列表的数据
                }else{
                  //this.$message.error("系统繁忙，请稍后重试!!![500]");
                  //打印后端的错误信息
                  this.$message.error(res.data.message);
                }
              }).catch(res=>{
                this.saveLoading2= false;  //关闭加载框
                this.saveFormVisible2 = false; //关闭对话框
                this.$refs.saveForm2.clearValidate();//清除红色提示
                this.$message.error("系统繁忙，请稍后重试!!![400,404]");
              })
            });
          }
        });
      },

      getDictionarytypes() {
        this.$http.get("/systemdictionarytype").then(res => {
          if (res) {
            this.dictionarytypes = res.data;
          }
        })
      },


      //11.抽屉列表---数据字典明细表展示方法+id -----第一种方法
      loadRowDrawerDataByQuery2(id) {
        console.log(id);
        this.$http.get("/systemdictionarytype/loadquery/"+id).then((res => {
          this.SystemdictionarydetailData = res.data
        }))
      },



      //1.抽屉列表---数据字典明细表展示方法+id-----第二种方法
      loadRowDrawerDataByQuery(id) {

        console.log(id);//id是明细表的type——id
        this.$http.get("/systemdictionarydetail/loadquery/"+id).then((res => {
          this.SystemdictionarydetailData = res.data;
          this.totals2=res.data.totals;    //计算总条数
          this.listLoading2 = false;       //关闭加载框
        }))
      },







      //计数用的
      handleChange(value) {
        console.log(value);
      }
		},
		mounted() {
		  //页面加载就调用这个方法
			this.getSystemdictionarytypes();
		}
	}

</script>

<style scoped>

</style>